package com.haredot.typehandler;

import com.haredot.utils.ConvertorIpUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 自定义 一个 IP 类型处理器
 */
public class IPTypeHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        Long aLong = ConvertorIpUtils.convertIpToNumber(parameter);
        ps.setLong(i, aLong);
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        long aLong = rs.getLong(columnName);
        //if (aLong == 0) return null ;
        return ConvertorIpUtils.convertNumberToIp(aLong);
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        long aLong = rs.getLong(columnIndex);
        //if (aLong == 0) return null ;
        return ConvertorIpUtils.convertNumberToIp(aLong);
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        long aLong = cs.getLong(columnIndex);
        return ConvertorIpUtils.convertNumberToIp(aLong);
    }
}
